package com.xywh.wanandroid.viewmodel

import com.xywh.wanandroid.MyApp
import com.xywh.wanandroid.R
import com.xywh.wanandroid.ext.getResString
import com.xywh.wanandroid.ext.requestCompose
import com.xywh.wanandroid.ext.showToastMsg
import com.xywh.wanandroid.network.apiService
import dagger.hilt.android.lifecycle.HiltViewModel
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import javax.inject.Inject

@HiltViewModel

class RegisterComposeViewModel @Inject constructor() : BaseViewModel() {

    fun registerWanAndroid(
        username: String,
        password: String,
        passwordAgain: String,
        navigateUp: () -> Unit
    ) {

        val context = MyApp.instance

        when {
            username.isEmpty() -> showToastMsg(context.getString(R.string.hint_username))
            password.isEmpty() -> showToastMsg(context.getString(R.string.hint_password))
            passwordAgain.isEmpty() -> showToastMsg(context.getString(R.string.hint_password_again))
            password != passwordAgain -> showToastMsg(context.getString(R.string.hint_password_not_equals))
            else ->
                requestCompose(
                    {
                        apiService.registerWanAndroid(
                            username, password, passwordAgain
                        )
                    },
                    {
                        showToastMsg(getResString(R.string.register_success))
                        navigateUp.invoke()
                    },
                    { showToastMsg(it.errorMsg) },
                    true,
                    context.getString(R.string.register_loading_tip)
                )

        }
    }


}